<template>
	<view class="goods-list">
		<view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
			<my-goods :item="item"></my-goods>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [],
				total: 0,
				//节流，看是否处于请求数据中
				isLoading: false,
				queryObj: {
					// 查询关键词
					query: '',
					// 商品分类Id
					cid: '',
					// 页码值
					pagenum: 1,
					// 每页显示多少条数据
					pagesize: 10
				}
			}
		},
		methods: {
			//获取商品数据
			async getGoodsList(){
				//打开节流阀
				this.isLoading = true
				const { data : res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				//关闭节流阀
				this.isLoading = false
				
				// cb&&cb()
				
				if(res.meta.status !== 200) return uni.$showMsg()
				// this.goodsList = res.message.goods
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
				// console.log(res)
			},
			
			// 点击进入详情页
			gotoDetail(item){
				uni.navigateTo({
				    url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			}
		},
		onLoad(options) {
			// 将页面参数转存到 this.queryObj 对象中
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		
		//触底加载事件
		onReachBottom(){
			// 看数据是否全部加载完毕
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total)
				return uni.$showMsg('数据加载完毕！')
				
			// 判断是否正在请求其它数据，如果是，则不发起额外的请求
			if(this.isLoading)
				return
			this.queryObj.pagenum += 1
			this.getGoodsList()
		},
		
		// 下拉刷新事件
		onPullDownRefresh() {
		  // 1. 重置关键数据
		  this.queryObj.pagenum = 1
		  this.total = 0
		  this.isloading = false
		  this.goodsList = []
		  
		  this.getGoodsList()
		  
		  uni.stopPullDownRefresh()
		  
		  // 2. 重新发起请求
		  // this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
	.goods-list{
		background-color: #fff;
	}
	
</style>
